<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Db;
use think\Session;
use think\Cookie;

class bookshelf extends controller
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //已购买
        !empty(session::get('user'))? $uid = session::get('user.id') : $uid = cookie::get('loginid');
        $usertel = Db::table('s75_user')->field(['tel','id'])->where('id',$uid)->find();
        $this->assign('usertel',$usertel);
        $list = Db::field('d.id as did,d.name,d.face,o.order_number,o.pay_money,o.status,otime')
                ->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.status=1')->where('o.uid='.$uid)
                ->select();
        $this->assign('count',count($list));
        foreach ($list as $k => $v) {
            $list[$k]['see'] = '已购买';
        }

        //未购买
        $nomoney = Db::field('d.name,d.face,o.order_number,o.pay_money,o.status,otime,o.id')
                ->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.status=0')->where('o.uid='.$uid)
                ->select();
        $this->assign('buycount',count($nomoney));
        $this->assign('nomoney',$nomoney);
        //订单
        $order = Db::field('d.name,d.face,o.order_number,o.pay_money,o.status,otime')
                ->table(['s75_data'=>'d','s75_orders'=>'o'])
                ->where('d.id=o.cid')->where('o.uid='.$uid)
                ->select();
       
        //已收藏
        $data = Db::field('d.id as did,d.name,d.face,c.id')
                ->table(['s75_data'=>'d','s75_collect'=>'c'])
                ->where('d.id=c.cid')->where('c.uid='.$uid)
                ->select();
        $this->assign('collectcount',count($data));
         $this->assign('data',$data);
        foreach ($data as $k => $v) {
            $data[$k]['see'] = '已收藏';
        }
        //去除重复值
        foreach ($list as $k => $v) {
            foreach ($data as $key => $value) {
                if($v['name'] == $value['name']){
                    unset($data[$key]);
                }
            }
        }


        //todo:

        $user = Session::get('user');
        $mybook = Db('data')->where('author', $user['name'])->select();


        $info = array_merge($list, $data);
        // var_dump($info);die;
        $this->assign('allcount', count($info));
        $this->assign('info', $info);
        $this->assign('order', $order);
        $this->assign('list', $list);
        $this->assign('mybook', $mybook);

        return $this->fetch('order/index');
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $result = Db::table('s75_collect')->where('id',$id)->delete(); 
          if($result) {
            $info['status'] = 'true';
            $info['info'] = '删除成功';
          }else{
            $info['status'] = 'false';
            $info['info'] = '删除失败';
          }
          return json($info);
    }

    public function deleteorder($id)
    {
        $result = Db::table('s75_orders')->where('id',$id)->delete(); 
          if($result) {
            $info['status'] = 'true';
            $info['info'] = '删除成功';
          }else{
            $info['status'] = 'false';
            $info['info'] = '删除失败';
          }
          return json($info); 
    }

    public function vippay(Request $request)
    {
      $data = $request->post();
      $uid = $data['uid'];
      $res = db('user')->where('id',$uid)->update(['vip'=>1]);
      $result = db('user')->where('id',$uid)->where('vip',1)->find();
      if($result){
        $info['status'] = false;
        $info['info'] = '你已经开通新芽会员';
      }else{
      if($res){
        $info['status'] = true;
        $info['info'] = '恭喜你，已开通新芽会员';
      }else{
        $info['status'] = false;
        $info['info'] = '开通失败';
      }
      }
      return json($info);
    }

}
